package com.aiit.carinsurance2.controller;

import com.aiit.carinsurance2.pojo.Carinsurance;
import com.aiit.carinsurance2.pojo.User;
import com.aiit.carinsurance2.pojo.Userinsurancepay;
import com.aiit.carinsurance2.service.CarinsuranceService;
import com.aiit.carinsurance2.service.UserinsurancepayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CarinsuranceController {

    @Autowired
    private CarinsuranceService carinsuranceService;
    @Autowired
    private UserinsurancepayService userinsurancepayService;

    @GetMapping("check")
    public ModelAndView list(HttpSession session){
        Object obj = session.getAttribute("user");
        User u = (User)obj;
        Integer uid = u.getUid();
        System.out.println(uid);
        List<Carinsurance> list = carinsuranceService.query(uid);
//        System.out.println(list);

        Double insurancepay = list.get(0).getInsurancepay();
        Double insurancemoney = list.get(0).getInsurancemoney();
        // System.out.println(insurancepay);
        // System.out.println(insurancemoney);
        Userinsurancepay uip = userinsurancepayService.querybyid(uid);

        if(uip!=null){

        }else {
            int insert = userinsurancepayService.insert(uid, insurancepay, insurancemoney);
        }

        ModelAndView mav = new ModelAndView();
        List<Carinsurance> carinsurancechoose1 = carinsuranceService.querycarinsurancechoose1(uid);
        Userinsurancepay carinsurancechoose2 = carinsuranceService.querycarinsurancechoose2(uid);

        mav.addObject("carinsurancechoose1",carinsurancechoose1);
        mav.addObject("carinsurancechoose2",carinsurancechoose2);
        mav.setViewName("check");
        return mav;
    }

}
